import 'dart:math';

import 'package:flutter/material.dart';

class StateDemo extends StatelessWidget {
  // 2. 设置默认值
  const StateDemo({
    super.key,
    this.title = '',
    this.content = '',
    this.tColor = Colors.orange,
    this.cColor = Colors.yellow,
  });

  // 1. 定义属性
  final String title;
  final String content;
  final Color tColor;
  final Color cColor;

  // 重写build 返回需要封装的组件即可
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.purple,
          title: Center(
            child: Text(
              '标题为:$title',
              style: TextStyle(
                  color: tColor, fontSize: 30, fontWeight: FontWeight.w900),
            ),
          ),
        ),
        // 1.2 主体部分
        body: MyStateWidget(),
      ),
    );
  }
}

// 1. 继承自StatefulWidget
class MyStateWidget extends StatefulWidget {
  const MyStateWidget({super.key});

  @override
  // 返回  _MyStateWidgetState 实例对象
  State<MyStateWidget> createState() => _MyStateWidgetState();
}

// 2. 继承自 State 并传入上一步的类，重写 build 实现 结构
class _MyStateWidgetState extends State<MyStateWidget> {
  num count = 0;
  // 结构在这里写
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          ElevatedButton(
              onPressed: () {
                // ignore: avoid_print
                print('点我干啥~');
                setState(() {
                  count--;
                });
              },
              child: const Text('递减')),
          Text(
            '个数为 $count',
            style: const TextStyle(fontSize: 30, fontWeight: FontWeight.w900),
          ),
          ElevatedButton(
              onPressed: () {
                // ignore: avoid_print
                print('点我干啥~');
                setState(() {
                  count = 0;
                });
              },
              child: const Text('归零')),
          ElevatedButton(
              onPressed: () {
                // ignore: avoid_print
                print('点我干啥~');
                setState(() {
                  count++;
                });
              },
              child: const Text('累加'))
        ],
      ),
    );
  }
}
